//
//  CommunityPartyViewController.m
//  homeland
//
//  Created by caesar on 14-7-31.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//

#import "CommunityPartyViewController.h"
#import "ListViewController.h"

@interface CommunityPartyViewController ()

@end

@implementation CommunityPartyViewController
@synthesize entity;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //isHaveEntity 为 yes :要请求后台加载， 否则： 直接以传进的实体展示
    NSString * isHaveEntity = [self.config objectForKey:@"isHaveEntity"];
    if ([isHaveEntity isEqualToString:@"YES"]) {
        [self getPatyInfo] ;
    }else{
        entity = [self.config objectForKey:@"dataItem"];
        [self initViewData:NO];
    }
    
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)getPatyInfo
{
    [self startLoadingViewWithText:@"正在载入..."];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    NSString *URL = [SERVER_URL_PARTY_ONE_INFO stringByAppendingFormat:@"%@",[self.config objectForKey:@"communityId"]];
    MKNetworkEngine *currentEngine = [[MKNetworkEngine alloc] initWithHostName:URL  customHeaderFields:nil];
    MKNetworkOperation *op = [currentEngine operationWithPath:@"" params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
        
        [self stopLoadingView];
        NSDictionary *json = [completedOperation responseJSON];
        entity = getObjectFromDic([PartyEntity class], json);
        [self initViewData:YES];
    } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
        [self stopLoadingView];
        [self.view makeToast:@"请求失败" duration:2.0 position:@"center"];
    }];
    [currentEngine enqueueOperation:op];
}
-(void)initViewData:(BOOL*)isRightBarButton{
    self.title = entity.partyName;
    
    self.contentTV.text = entity.partyContent;
    
    [self.phoneBtn setTitle:entity.partyMobile forState:UIControlStateNormal];
    self.addressLable.text = entity.partyAddress;
    
    self.contentTV.userInteractionEnabled = NO;
    if (isRightBarButton) {
        if(entity.isGeneralParty == 1){
            //生成导般右侧按据
            self.navigationItem.rightBarButtonItem = rightNavBtnWithImgName(@"支部", self, @selector(rightBtAction:));
            self.navigationItem.rightBarButtonItem = rightNavButton(self, @selector(rightNavButton), @"支部");
        }
    }
    
    //调整 位置
    CGFloat contentHeight = [self caculateHeightForString:self.contentTV.text AndFontSize:14.0f AndWitdh:300.0f];
    
    self.contentTV.frame = CGRectMake(10, CGRectGetMaxY(self.curLine.frame)+10 , 300, contentHeight);
    self.myView2.frame = CGRectMake(10, CGRectGetMaxY(self.contentTV.frame)+10, 300, 155);
    CGFloat totalHeight = MAX(CGRectGetMaxY(self.myView2.frame)+10, CGRectGetHeight(self.scrollView.frame));
    self.myView.frame = CGRectMake(0.0, 0.0, 320.0, totalHeight);
    [self.scrollView addSubview: self.myView];
    self.scrollView.contentSize = self.myView.frame.size;
}

- (IBAction)callPhone:(id)sender {
    NSString* numberAfterClear =self.phoneBtn.titleLabel.text;
    
    if (![[CommonUtil dealNullObject:numberAfterClear ] isEqualToString:@""]) {
        NSURL *phoneNumberURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", numberAfterClear]];
        [[UIApplication sharedApplication] openURL:phoneNumberURL];
    }
    
}
#pragma mark - Button Methods
-(void)rightNavButton
{
    ListViewController *crVC =[[ListViewController alloc]initWithNibName:@"ListViewController" bundle:nil];
    NSDictionary *dic = @{@"key":@"社区党组织-支部",@"type":@"2", @"entity":entity,@"title":entity.partyName};
    
    crVC.config = dic;
    [self.navigationController pushViewController:crVC animated:YES];

}
@end
